package me.wangxuan.pat.processor;

import java.util.Map;
import java.util.logging.Logger;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SignUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import me.wangxuan.pat.entity.Account;

public class OurPayHttpProcessor implements IHttpProcessor {
	private static final Logger logger = Logger.getLogger("global");
	@Override
	public Map<String, Object> buildReqParams(Map<String, Object> params, Account a) {
		if("MD5".equalsIgnoreCase((String) params.get("signType"))) {
			StringBuilder sb = new StringBuilder();
			JSONObject cfg = JSON.parseObject(a.getConfigs());
//			logger.info("cfg:"+cfg);
			sb.append("&key=").append(cfg.getString("md5Key"));
			String signStr = MapUtil.sortJoin(params, "&", "=", true,sb.toString());
			logger.info("signStr:"+signStr);
			String sign = new Digester(DigestAlgorithm.MD5).digestHex(signStr);
			logger.info("sign:"+sign);

//			String sign = SignUtil.signParams(DigestAlgorithm.MD5, params, "&", "=", true, sb.toString());
//			logger.info("sign:"+sign);
			
			params.put("sign", sign);
			
			return params;
		}
		return null;
	}

	@Override
	public JSONObject parseResponse(String resp) {
		return null;
	}
}
